
//
//  CMDBUserCenterViewController.swift
//  CMDB
//
//  Created by 曹桂祥 on 17/5/3.
//  Copyright © 2017年 CMDB. All rights reserved.
//

import UIKit

private let kTitleViewH : CGFloat = 44

private let KTopViewH : CGFloat = 0.5 * CMDB_Screen_Width

private let ratio : CGFloat = 0.86

class CMDBUserCenterViewController: CMDBFullContentViewController {
    
    var topView         : UIView!
    
    var userContentView : UIView!
    
    var iconImageView   : UIImageView!
    
    var tagsView        : UIView!
    
    var currVCIndex     : Int = 0
    
    var currentVc : CMDBBaseViewController!
    
    var thisChildVCs   : [UIViewController] = [UIViewController]()
    
    var attriBtnsArr : [UIButton] = [UIButton]()
    
    var contentScrollView : UIScrollView!
    
    var childVcs : [UIViewController]!
    
    var thisTableView : CMDBUserCenterProductionTableView?
    
    lazy var editBtn : UIButton = UIButton()
    
    lazy var popAnimator : PopEditPopAnimator = PopEditPopAnimator{ [weak self] (presented)->() in
        
        self!.editBtn.selected = presented
        
    }
    
    // MARK:- 懒加载属性
    lazy var pageTitleView : CMDBPageTitleView = {[weak self] in
        
        let titleFrame = CGRect(x: 0, y: CMDB_Screen_Width * 0.5, width: CMDB_Screen_Width, height: kTitleViewH)
        
        let titles = ["作品", "资料", "档期","动态", "日记", "相册"]
        
        let titleView = CMDBPageTitleView(frame: titleFrame, titles: titles)
        
        titleView.titleFont = CMDB_16_FONT
        
        titleView.backgroundColor = UIColor.Hex(0xfdfdfd)
        
        titleView.delegate = self
        
        return titleView
        }()
    
    
    lazy var pageContentView : CMDBPageContentView = {[weak self] in
        
        // 1.确定内容的frame
        let contentH = CMDB_Screen_Height - kTitleViewH - 0.5 * CMDB_Screen_Width
        let contentFrame = CGRect(x: 0, y: kTitleViewH + 0.5 * CMDB_Screen_Width, width: CMDB_Screen_Width, height: contentH)
        
        // 2.确定所有的子控制器
        self!.childVcs = [UIViewController]()
        
        let productVC = CMDBUserCenterProductionViewController()
        
        productVC.containerVc = self
        
        self!.childVcs.append(productVC)
        self!.childVcs.append(CMDBUserCenterProfileViewController())
        self!.childVcs.append(CMDBUserCenterCalendarViewController())
        
        for i in 1 ..< 3{
            self!.childVcs.append(CMDBBaseViewController())
        }
        
       self!.childVcs.append(CMDBUserCenterPhotoLibraryViewController())
        
        let contentView = CMDBPageContentView(frame: contentFrame, childVcs: self!.childVcs, parentController: self)
        contentView.delegate = self
        return contentView
        }()
    
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        
        self.navigationController!.view.frame = CGRect(x: 0, y: 0, width: CMDB_Screen_Width, height: CMDB_Screen_Height)
    }
    
    
    override func buildTitleView() {
        super.buildTitleView()
        
        titleView?.backgroundColor = UIColor.clearColor()
        
        let manageButton                    = UIButton(frame : CGRect(x: width - 100, y: 0, width: 100, height: 64))
        manageButton.center                 = CGPoint(x: width - 20, y: self.titleView!.middleY + 10)
        manageButton.imageView?.contentMode = .Center
        manageButton.setImage(UIImage(named: "Shape"),     forState: .Normal)
        manageButton.setImage(UIImage(named: "Shape"), forState: .Highlighted)
        
        self.titleView?.addSubview(manageButton)
        
        manageButton.addTarget(self, action: #selector(manageClcik), forControlEvents: .TouchUpInside)
        
    }

    
    override func buildContentView() {
        super.buildContentView()
        
        buildContentScrollView()
        
        buildTopView()
        
        buildUserContentView()
        
        loadData()
    }
    
}

//设置UI
extension CMDBUserCenterViewController{
    
    func buildContentScrollView(){
        
        contentScrollView = UIScrollView.getInstance()
        
        contentScrollView.delegate = self
        
        contentScrollView.frame = CGRect(x: 0, y: 0, width: CMDB_Screen_Width, height: CMDB_Screen_Height)
        
        contentScrollView.contentSize = CGSize(width: 0, height: 2000)
        
        contentScrollView.scrollIndicatorInsets = UIEdgeInsets(top: KTopViewH + 44, left: 0, bottom: 0, right: 0)
        
        contentView?.addSubview(contentScrollView)
    }

    
    // 1.设置顶部
    func buildTopView(){
        
        topView                           = UIView(frame: CGRect(x: 0, y: 0, width: width, height: width * 0.5))
        
        //1.backgrounfView
        let backImageView                 = UIImageView(frame: topView.bounds)
        
        backImageView.image               = UIImage(named: "Rectangle_24")
        
        //2.iconView
        iconImageView                     = UIImageView(frame: CGRect(x: KleftMargin, y: 97, width: 150 * CMDB_Rate, height: 150 * CMDB_Rate))
        
        iconImageView.image               = UIImage(named: "test头像")
        
        iconImageView.layer.cornerRadius  = 5
        
        iconImageView.layer.masksToBounds = true
        
        iconImageView.layer.borderColor   = UIColor.whiteColor().CGColor
        
        iconImageView.layer.borderWidth   = 0.5
        
        //3.nameLabel
        let nameLabel                     = UILabel.getInstance()
        
        var testStr                       = "冯晓雷"
        
        nameLabel.text                    = testStr
        
        nameLabel.textColor               = UIColor.whiteColor()
        
        nameLabel.font                    = CMDB_16_Bold_FONT
        
        let size                          = testStr.suitToSize(CGSize(width: 999, height: 999), boldFontSize: 16, lineSpacing: 1)
        
        nameLabel.frame                   = CGRect(x: CGRectGetMaxX(iconImageView.frame) + 10, y: CGRectGetMinY(iconImageView.frame) + 21, width: size.width, height: size.height)
        
        //4.性别icon
        let sexImageView                  = UIImageView(frame: CGRect(x: CGRectGetMaxX(nameLabel.frame) + 6, y: CGRectGetMinY(iconImageView.frame) + 25, width: 24 * CMDB_Rate, height: 24 * CMDB_Rate))
        
        sexImageView.image                = UIImage(named: "男_icon")
        
        //5.年龄label
        let ageLabel                      = UILabel.getInstance()
        
        ageLabel.textColor                = UIColor.whiteColor()
        
        ageLabel.font                     = CMDB_12_FONT
        
        testStr                           = "29岁"
        
        let ageSize                       = testStr.suitToSize(CGSize(width: 999, height: 999), fontSize: 12, lineSpacing: 1)
        
        ageLabel.text                     = testStr
        
        ageLabel.frame                    = CGRect(x: CGRectGetMaxX(sexImageView.frame) + 6, y: CGRectGetMinY(iconImageView.frame) + 25, width: ageSize.width, height: ageSize.height)
        
        //6.生成简历
        let resumeBtn                     = UIButton(title: "生成简历", borderColor: UIColor.whiteColor(), titleSizeFont: CMDB_12_FONT, isShowBorder: true)
        
        resumeBtn.layer.cornerRadius      = 2
        
        resumeBtn.setTitleColor(UIColor.whiteColor(), forState: .Normal)
        
        topView.addSubview(backImageView)
        
        topView.addSubview(iconImageView)
        
        topView.addSubview(ageLabel)
        
        topView.addSubview(nameLabel)
        
        topView.addSubview(sexImageView)
        
        topView.addSubview(resumeBtn)
        
        resumeBtn.snp_makeConstraints { (make) in
            make.right.equalTo(-15);
            make.bottom.equalTo(-15);
            make.width.equalTo(150 * CMDB_Rate);
            make.height.equalTo(40 * CMDB_Rate);
        }
        
        contentScrollView?.addSubview(topView)
        
        //设置attributes
        setupAttributesLabel()
        
        //设置关注/粉丝
        setSocicalBtns()
    }
    
    // 2.设置分块
    func buildUserContentView(){
        
        contentScrollView?.addSubview(pageTitleView)
        
        contentScrollView?.addSubview(pageContentView)
    }
}

extension CMDBUserCenterViewController{
    
    //设置attributes
    func setupAttributesLabel(){
        
        tagsView = UIView(frame: CGRect(x: CGRectGetMaxX(iconImageView.frame) + 10, y: CGRectGetMinY(iconImageView.frame) + 46, width: 200, height: 16))
        
        topView.addSubview(tagsView)
        
        for i in 0 ..< 3 {
            
            let attributteLabel = UIButton(title: "演员", borderColor: UIColor.whiteColor(), titleSizeFont: CMDB_11_FONT, isShowBorder: true)
            
            attributteLabel.layer.cornerRadius = 2
            
            attributteLabel.setTitleColor(UIColor.whiteColor(), forState: .Normal)
            
            attributteLabel.frame = CGRect(x: (30 + 5) * i, y: 0, width: 30, height: 15)
            
            attriBtnsArr.append(attributteLabel)
            
            tagsView.addSubview(attributteLabel)
            
        }
        
        let lastAttrBtn = attriBtnsArr.last!
        
        editBtn.setImage(UIImage(named: "编写"), forState: .Normal)
        
        editBtn.sizeToFit()
        
        editBtn.frame = CGRect(x: CGRectGetMaxX(lastAttrBtn.frame) + 5, y: -2, width: editBtn.width, height: editBtn.height)
        
        tagsView.addSubview(editBtn)
        
        editBtn.addTarget(self, action: #selector(editBtnClick), forControlEvents: .TouchUpInside)
        
    }
    
    //设置关注/粉丝
    func setSocicalBtns(){
        
        let socialView = UIView(frame:CGRect(x: CGRectGetMaxX(iconImageView.frame) + 10, y: CGRectGetMinY(iconImageView.frame) + 66, width: 200, height: 16))
        
        topView.addSubview(socialView)
        
        let concernBtn = CMDBSocialHorBtn(frame: CGRect(x: 0, y: 1, width: 50, height: 15))
        
        concernBtn.nameLabel.text = "关注"
        
        concernBtn.numLabel.text = "100"
        
        socialView.addSubview(concernBtn)
        
        let singleView = UIView.CreateLine(CGRect(x: CGRectGetMaxX(concernBtn.frame) + 5, y: 2, width: 0.5, height: 11), lineColor: UIColor.whiteColor())
        
        socialView.addSubview(singleView)
        
        let fansBtn = CMDBSocialHorBtn(frame: CGRect(x: CGRectGetMaxX(concernBtn.frame) + 10, y: 1, width: 50, height: 15))
        
        fansBtn.nameLabel.text = "粉丝"
        
        fansBtn.numLabel.text = "100"
        
        socialView.addSubview(fansBtn)
    }
    
    override func popSelf() {
        
        self.popViewControllerAnimated(true)
        
        NSNotificationCenter.defaultCenter().postNotificationName(ShowDismissFrameNote, object: nil)
    }
    
}

// 请求数据
extension CMDBUserCenterViewController{
    
    func loadData(){
        
        
    }
}

//MARK:- action method
extension CMDBUserCenterViewController{
    
    @objc func manageClcik(){
        
        let currentVc = pageContentView.childVcs[currVCIndex]
        
        switch currVCIndex {
        case 0:
            
            let vc = currentVc as! CMDBUserCenterProductionViewController
            
            
            
            break
        case 1:
            printLog(1)
        default:
            printLog("default")
        }
    }
}


extension CMDBUserCenterViewController : UIScrollViewDelegate {
    
    func scrollViewWillEndDragging(scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {
        
        // 判断松手时是否刷新
        let y = scrollView.contentOffset.y

        if y < -65 {

            if currVCIndex == 0 {

                let currentVc = childVcs[currVCIndex] as! CMDBUserCenterProductionViewController

                thisTableView = currentVc.tableView!

                thisTableView?.mj_header.beginRefreshing()

            }else{
                
                
            }

        }else if y > 0 && y <= KTopViewH{
            
            functionViewAnimation(offsetY: y)
            
        }
    }
    
    func functionViewAnimation(offsetY y:CGFloat){
        
        if y > 0.5 * KTopViewH {
            contentScrollView.setContentOffset(CGPoint(x:0,y:KTopViewH), animated: true)
        }else{
            contentScrollView.setContentOffset(CGPoint(x:0,y:0), animated: true)
        }
        
    }
    
    func scrollViewDidScroll(scrollView: UIScrollView) {
        
        let y = scrollView.contentOffset.y
        
        printLog(y)
        
        if y <= 0 { //下滑
            
            var topViewNewF = topView.frame
            
            topViewNewF.origin.y = y
            
            self.topView.frame = topViewNewF
            
            pageTitleView.frame = CGRect(x: 0, y: CGRectGetMaxY(self.topView.frame), width: CMDB_Screen_Width, height: pageTitleView.height)
            
            pageContentView.frame = CGRect(x: 0, y: CGRectGetMaxY(self.pageTitleView.frame), width: CMDB_Screen_Width, height: pageContentView.height)
            
            // 偏移量给tableView
            if currVCIndex == 0 {
                
                let currentVc = childVcs[currVCIndex] as! CMDBUserCenterProductionViewController
                
                thisTableView = currentVc.tableView!
                
                thisTableView?.mj_header.beginRefreshing()
                
            }else{
                
                
                
            }

            
            
            
        } else if y < KTopViewH + 44 && y > 0{
            
            
        
        }
//
        
        
        
    }

}




//MARK:- 处理actionSheet
extension CMDBUserCenterViewController{
    
    
}

//MARK:- 点击事件
extension CMDBUserCenterViewController{
    
    @objc func editBtnClick(){
        
        let popEditViewController = CMDBProductionEditViewController()
        
        popEditViewController.modalPresentationStyle = .Custom
        
        popEditViewController.transitioningDelegate  = popAnimator
        
        popAnimator.presentedFrame = CGRect(x: KleftMargin, y: 0.25 * CMDB_Screen_Height, width: CMDB_Screen_Width - 2 * KleftMargin, height: 0.55 * CMDB_Screen_Height)
        
        presentViewController(popEditViewController, animated: true, completion: nil)
        
    }
        
}

// MARK:- PageTitleViewDelegate协议
extension CMDBUserCenterViewController : CMDBPageTitleViewDelegate{
    
    func pageTitleView(titleView: CMDBPageTitleView, selectedIndex index: Int) {
        
        pageContentView.setCurrentIndex(index)
        
        currVCIndex = index
    }
}

// MARK:- PageContentViewDelegate协议
extension CMDBUserCenterViewController : CMDBPageContentViewDelegate {
    
    func contentView(titleView: CMDBPageContentView, progress: CGFloat, sourceIndex: Int, targetIndex: Int) {
        
        currVCIndex = targetIndex
        
        pageTitleView.setTitleWithProgress(progress, sourceIndex: sourceIndex, targetIndex: targetIndex)
        
    }
}


